package com.hdumooc.jdbc;

import java.sql.*;

public class JDBCDemo03 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            // 1.注册驱动(可以省略)
            // 2.获取连接(url, username, password)
            String url = "jdbc:mysql://localhost:3306/jdbc_lessons?characterEncoding=UTF-8";
            connection = DriverManager.getConnection(url, "root", "root");
            // 3.获取SQL执行对象 Statement
            statement = connection.createStatement();
            // 4.执行DQL操作 executeQuery
            String sql = "SELECT * FROM jdbc_user;";
            resultSet = statement.executeQuery(sql);
            // 5.处理结果集
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != resultSet)
                    resultSet.close();
                if (null != statement)
                    statement.close();
                if (null != connection)
                    connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
